const mysql = require('mysql');
const config = {
    database: 'notebook',
    user: 'root',
    password: 'remover',
    timezone: 'SYSTEM'
}
const UserService = {
    login: (account, password) => {
        sql = 'select id,nickname from t_users where account = ? and password = ? ;'
        sqlParams = [account, password];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log('这是登录结果，务必看这儿', res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    register: (account, password, nickname) => {
        sql = 'insert into t_users set account = ? ,password = ? ,nickname = ? ;'
        sqlParams = [account, password, nickname];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            if (res.insertId > 0) {
                                resolve(1);
                            } else {
                                resolve(0);
                            }
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    },
    registerValidate: (account) => {
        sql = 'select * from t_users where account = ? ;'
        sqlParams = [account];
        return new Promise((resolve, reject) => {
            const pool = mysql.createPool(config);
            pool.getConnection((err, conn) => {
                if (!err) {
                    conn.query(sql, sqlParams, (e, results) => {
                        if (!e) {
                            let res = JSON.parse(JSON.stringify(results));
                            console.log(res);
                            resolve(res);
                            conn.destroy();
                        } else {
                            console.log("sql:", e);
                            reject(e);
                        }
                    })
                } else {
                    console.log("conn err:", err);
                    reject(err);
                }
            })
        })
    }
}
module.exports = UserService